package Game;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import Server.Client;

public class Challange {

	private String send;
	private double timeTosend;
	private Client client;
	private int timeFromser;
	private int problem;
	private static List<String> word;
	private static String read;
	private static BufferedReader reader;
	/**
	 * This is the part of add the dictonary word.
	 */
	static {
		word = new ArrayList<String>();
		final String DICT_FILE = "/dictionary/dictionary.txt";
		reader = new BufferedReader(new InputStreamReader(Challange.class.getResourceAsStream(DICT_FILE)));
		try {
			read = reader.readLine();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		while (read != null) {
			word.add(read);
			try {
				read = reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * This is the method for the challenge
	 * @param client
	 * @param problem
	 */
	public Challange(Client client, int problem){
		this.problem = problem;
		this.client = client;
		try {
			client.sendToServer(word.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		send = sendWord();
	}
	/**
	 * This method to get and send the word that random.
	 * @return the word
	 */
	public String sendWord() {
		String send = word.get(problem);
		return send;
	}
	/**
	 * This method is sending the time to the server.
	 * @param time the time that count.
	 * @throws IOException
	 */
	public void sendTimetoServer(double time) throws IOException {
		this.timeTosend = time;
		client.sendToServer(new Double(timeTosend));
	}
	/**
	 * This is the method that get time from the server.
	 * @param fromServer time from the server
	 */
	public void timeFromServer(int fromServer) {
		this.timeFromser = fromServer;
		word.remove(problem);
	}
	/**
	 * This method is give the time that user use to type.
	 * @return
	 */
	public int timetoUI() {
		return timeFromser;
	}
	
	public void setNewProblem(int problem) {
		this.problem = problem;
	}
}